package bc

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/bc"
	bcReq "github.com/flipped-aurora/gin-vue-admin/server/model/bc/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
)

type BcProjectService struct {
}

// CreateBcProject 创建bcProject表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcProjectService *BcProjectService) CreateBcProject(bcProject *bc.BcProject) (err error) {
	err = global.GVA_DB.Create(bcProject).Error
	return err
}

// DeleteBcProject 删除bcProject表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcProjectService *BcProjectService) DeleteBcProject(bcProject bc.BcProject) (err error) {
	err = global.GVA_DB.Delete(&bcProject).Error
	return err
}

// DeleteBcProjectByIds 批量删除bcProject表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcProjectService *BcProjectService) DeleteBcProjectByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]bc.BcProject{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateBcProject 更新bcProject表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcProjectService *BcProjectService) UpdateBcProject(bcProject bc.BcProject) (err error) {
	err = global.GVA_DB.Save(&bcProject).Error
	return err
}

// GetBcProject 根据id获取bcProject表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcProjectService *BcProjectService) GetBcProject(id uint) (bcProject bc.BcProject, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&bcProject).Error
	return
}

func (bcProjectService *BcProjectService) GetBcProjectAll() (list []bc.BcProject, err error) {
	var bcProjects []bc.BcProject
	err = global.GVA_DB.Find(&bcProjects).Error
	return bcProjects, err
}

func (bcProjectService *BcProjectService) GetBcProjectByName(id string) (bcProject bc.BcProject, err error) {
	err = global.GVA_DB.Where("project_name = ?", id).First(&bcProject).Error
	return
}

// GetBcProjectInfoList 分页获取bcProject表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcProjectService *BcProjectService) GetBcProjectInfoList(info bcReq.BcProjectSearch, nums []int) (list []bc.BcProject, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&bc.BcProject{})
	var bcProjects []bc.BcProject
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if len(nums) > 0 {
		db = db.Where("id in ?", nums)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&bcProjects).Error
	return bcProjects, total, err
}
